package com.leo.boot.config;

import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.ResourceHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;
import java.util.List;

@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
    /**WebMvcConfigurerAdapter在JDK8中这个类已经过时，我们直接继承这个类所继承的接口*/
//    @Bean
//    public FilterRegistrationBean timeFilter(){
//        FilterRegistrationBean registrationBean =new FilterRegistrationBean();
//        TimeLimitFilter timeFilter =new TimeLimitFilter();
//        registrationBean.setFilter(timeFilter);
//        registrationBean.addUrlPatterns("/*");
//        return registrationBean;
//    }
    /**
     * 增加swagger的支持
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/swagger-ui.html", "/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        // 设置虚拟目录
        registry.addResourceHandler("/files/**").addResourceLocations("file:"+ AppConst.fileRootpath);
        registry.addResourceHandler("/**").addResourceLocations("file:"+ AppConst.fileRootpath+"web"+File.separator);
    }
//    /**
//     * 添加拦截器
//     */
//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        //拦截路径可自行配置多个 可用 ，分隔开
//        registry.addInterceptor(new AuthInterceptor())
//                .excludePathPatterns("/swagger-resources","/swagger-resources/**","/webjars/**", "/null/**",
//                        "/**/*.js", "/**/*.html", "/**/*.css", "/**/*.woff2", "/**/*.png", "/**/*.gif","/**/*.jpg","/**/*.bpm","/**/*.xls","/**/*.xlsx","/**/*.doc","/**/*.docx",
//                        "/public/**",  "/test/**", "/files/**", "/swagger-ui.html", "/index.html", "/error", "/**/swagger*/**")
//                .addPathPatterns("/**");
//    }
    /**
     * 跨域支持
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "DELETE", "PUT", "PATCH", "OPTIONS", "HEAD")
                .maxAge(3600 * 24);
    }

    /**
     * RequestContextListener注册
     */
    @Bean
    public ServletListenerRegistrationBean<RequestContextListener> requestContextListenerRegistration() {
        return new ServletListenerRegistrationBean<>(new RequestContextListener());
    }
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(0, new MappingJackson2HttpMessageConverter());
        converters.add(1, new ResourceHttpMessageConverter());
    }

}